package com.ym.b2b.user.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.b2b.buyer.dao.EmCBuyerDao;
import com.ym.b2b.buyer.sqlbean.EmCBuyer;
import com.ym.b2b.commons.conf.AgentToken;
import com.ym.b2b.commons.conf.WebProviderTokenUser;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.provider.dao.EmCProviderDAO;
import com.ym.b2b.provider.sqlbean.EmCProvider;
import com.ym.b2b.salesman.dao.EmBSalesmanDAO;
import com.ym.b2b.user.dao.AgentUserDao;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.service.AgentUserService;
import com.ym.b2b.user.sqlbean.AgentBuyer;
import com.ym.b2b.user.sqlbean.AgentProvider;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Service
public class AgentUserServiceImpl extends BaseService implements AgentUserService {

    private Logger         logger = Logger.getLogger(getClass());

    @Autowired
    private AgentUserDao   agentUserDao;

    @Autowired
    private EmCUserDAO     userDao;
    @Autowired
    private EmCUserDAO     emCUserDAO;

    @Autowired
    private EmCBuyerDao    emCBuyerDao;

    @Autowired
    private EmCProviderDAO emCProviderDAO;

    @Autowired
    private EmBSalesmanDAO emBSalesmanDAO;

    @Override
    public Page<AgentBuyer> searchPageAgentBuyerList(AgentBuyer agentBuyer, Page<AgentBuyer> page) throws BaseException {
        try {
            return agentUserDao.searchPageAgentBuyer(agentBuyer, page);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询异常");
        }
    }

    @Override
    public Result doAgentBuyer(String code, AgentToken agentToken) throws BaseException {
        // NO.1 获取采购商信息 拼接处webtoken
        Result ret = new Result();
        EmCBuyer emCBuyer = new EmCBuyer();
        emCBuyer.setBuyerCode(code);
        EmCBuyer emCBuyer2 = emCBuyerDao.showEmCBuyerDetail(emCBuyer);

        WebTokenUser webTokenUser = new WebTokenUser();
        webTokenUser.setUserType(2);
        webTokenUser.setUserId(agentToken.getAgentId());
        webTokenUser.setLoginName(agentToken.getLoginName());
        webTokenUser.setUserName(agentToken.getAgentName());
        webTokenUser.setBuyerCode(code);
        webTokenUser.setPayType(emCBuyer2.getPayType() == null ? 0 : emCBuyer2.getPayType());
        webTokenUser.setBuyerIsValid(emCBuyer2.getIsValid());
        webTokenUser.setIsBranch(emCBuyer2.getIsBranch());
        webTokenUser.setBusinessStartDate(emCBuyer2.getBusinessStartDate());
        webTokenUser.setBusinessEndDate(emCBuyer2.getBusinessEndDate());
        webTokenUser.setLicenseStartDate(emCBuyer2.getLicenseStartDate());
        webTokenUser.setLicenseEndDate(emCBuyer2.getLicenseEndDate());

        webTokenUser.setSaleMan(emCBuyer2.getSalesman());

        ret.putData("user", webTokenUser);
        return ret;
    }

    @Override
    public Page<AgentProvider> searchPageAgentProviderList(AgentProvider agentProvider, Page<AgentProvider> page)
                                                                                                                 throws BaseException {
        try {
            return agentUserDao.searchPageAgentProvider(agentProvider, page);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("查询异常");
        }
    }

    @Override
    public Result doAgentProvider(String code, AgentToken agentToken) throws BaseException {
        // 检测是否存在供应商
        Result ret = new Result();
        EmCProvider emCProvider = new EmCProvider();
        emCProvider.setProviderCode(code);
        EmCProvider emCProvider2 = emCProviderDAO.showEmCProviderDetail(emCProvider);

        WebProviderTokenUser providerToken = new WebProviderTokenUser();
        providerToken.setUserType(1);
        providerToken.setUserId(agentToken.getAgentId());
        providerToken.setLoginName(agentToken.getLoginName());
        providerToken.setUserName(agentToken.getAgentName());
        providerToken.setProviderCode(code);
        providerToken.setLevelId(emCProvider2.getLevelId().intValue());
        providerToken.setSearchStartDate(emCProvider2.getSearchStartDate());

        ret.putData("providerUser", providerToken);
        return ret;
    }
}
